// 引入 web3.js 库
const Web3 = require('web3')

// 创建 web3.js 库实例对象
let myweb3 = new Web3(new Web3.providers.HttpProvider('HTTP://127.0.0.1:7545'))

// 获取合约的 ABI 文件转为 JSON 格式
let guessgameContractABI = JSON.parse(
  '[{"constant":false,"inputs":[{"name":"_player","type":"uint256"}],"name":"playGame","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getGuessResult","outputs":[{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"getRandom","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]'
)
// console.log(guessgameContractABI)

// 获取合约BIN文件（文件16进制数据）
let guessgameContractData =
  '0x60806040526001600055600180556001600255600060035534801561002357600080fd5b50610219806100336000396000f3006080604052600436106100565763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416635873533d811461005b5780636072d94b14610075578063aacc5a17146100a8575b600080fd5b34801561006757600080fd5b506100736004356100cf565b005b34801561008157600080fd5b5061008a6101a0565b60408051938452602084019290925282820152519081900360600190f35b3480156100b457600080fd5b506100bd6101ae565b60408051918252519081900360200190f35b600080821180156100e1575060038211155b151561014e57604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f706c6179657243686f69636520697320696e76616c6964206e756d6265722e00604482015290519081900360640190fd5b6101566101ae565b6001839055600281905590508181141561017457600160005561019c565b8181036001148061018757508082036002145b1561019657600260005561019c565b60036000555b5050565b600154600254600054909192565b60038054604080514281526c010000000000000000000000003302602082015260348101929092525190819003605401902060009190066001019050905600a165627a7a723058200595a05393159a57875ed3daa92d6d6f2803af5974099384a287eeceb04643da0029'

// 创建合约对象实例（参数：合约 ABI 文件）
let guessgameContract = new myweb3.eth.Contract(guessgameContractABI)
// console.log(guessgameContract)

// 部署合约
guessgameContract
  .deploy({
    data: guessgameContractData // 合约数据
  })
  .send(
    {
      from: '0xdc913Dc03d9Eb7AEc929B1389C88fa3BCd4D3CeF', // 部署合约的外部账户地址
      gas: 1500000,
      gasPrice: '1000000' // 每公里油费
    },
    (error, result) => {
      console.log(result) // 回调函数返回交易Hash地址
    }
  )
